﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;

namespace Kevin.SLUserControlLibrary
{
    public class CustomColorControl : ContentControl
    {
        public static readonly DependencyProperty DisplayColorProperty = DependencyProperty
         .Register("DisplayColor", typeof(SolidColorBrush), typeof(CustomColorControl)
         , new PropertyMetadata(new PropertyChangedCallback(CustomColorControl.OnDisplayColorChanged)));

        private static readonly DependencyProperty NewColorProperty = DependencyProperty
         .Register("NewColor", typeof(SolidColorBrush), typeof(CustomColorControl), null);

        public delegate void Submit(SolidColorBrush newColor);
        public event Submit OnSubmit;

        private Slider SliderR;
        private Slider SliderG;
        private Slider SliderB;
        private Slider SliderA;

        private TextBlock tbR;
        private TextBlock tbG;
        private TextBlock tbB;
        private TextBlock tbA;

        private Button SubmitButton;

        public SolidColorBrush DisplayColor
        {
            get { return (SolidColorBrush)GetValue(CustomColorControl.DisplayColorProperty); }
            set { SetValue(CustomColorControl.DisplayColorProperty, value); }
        }

        private SolidColorBrush NewColor
        {
            get { return (SolidColorBrush)GetValue(CustomColorControl.NewColorProperty); }
            set { SetValue(CustomColorControl.NewColorProperty, value); }
        }

        public CustomColorControl()
        {
            this.DefaultStyleKey = typeof(CustomColorControl);
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            this.SliderR = base.GetTemplateChild("SliderR") as Slider;
            this.SliderG = base.GetTemplateChild("SliderG") as Slider;
            this.SliderB = base.GetTemplateChild("SliderB") as Slider;
            this.SliderA = base.GetTemplateChild("SliderA") as Slider;

            this.tbR = base.GetTemplateChild("tbR") as TextBlock;
            this.tbG = base.GetTemplateChild("tbG") as TextBlock;
            this.tbB = base.GetTemplateChild("tbB") as TextBlock;
            this.tbA = base.GetTemplateChild("tbA") as TextBlock;

            this.SubmitButton = base.GetTemplateChild("Submit") as Button;
            this.SubmitButton.Click += new RoutedEventHandler(SubmitButton_Click);

            this.SliderR.ValueChanged += new RoutedPropertyChangedEventHandler<double>(SliderR_ValueChanged);
            this.SliderG.ValueChanged += new RoutedPropertyChangedEventHandler<double>(SliderR_ValueChanged);
            this.SliderB.ValueChanged += new RoutedPropertyChangedEventHandler<double>(SliderR_ValueChanged);
            this.SliderA.ValueChanged += new RoutedPropertyChangedEventHandler<double>(SliderR_ValueChanged);

            if (this.DisplayColor != null)
            {
                this.SliderR.Value = this.DisplayColor.Color.R;
                this.SliderG.Value = this.DisplayColor.Color.G;
                this.SliderB.Value = this.DisplayColor.Color.B;
                this.SliderA.Value = this.DisplayColor.Color.A;
            }
        }

        void SubmitButton_Click(object sender, RoutedEventArgs e)
        {
            if (this.OnSubmit != null)
                this.OnSubmit(this.NewColor);

            Line line = new Line();

            DoubleCollection d = new DoubleCollection();

            d.Add(3d);
            d.Add(4d);
            line.StrokeDashArray = d;

        }

        void SliderR_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            this.tbR.Text = Math.Round(this.SliderR.Value).ToString();
            this.tbG.Text = Math.Round(this.SliderG.Value).ToString();
            this.tbB.Text = Math.Round(this.SliderB.Value).ToString();
            this.tbA.Text = Math.Round(this.SliderA.Value).ToString();

            Color color = Color.FromArgb((byte)this.SliderA.Value, (byte)this.SliderR.Value, (byte)this.SliderG.Value, (byte)this.SliderB.Value);
            this.NewColor = new SolidColorBrush(color);
        }

        private static void OnDisplayColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            CustomColorControl customColorControl = (CustomColorControl)d;

            if (customColorControl.SliderR != null && customColorControl.DisplayColor != null)
            {
                customColorControl.SliderR.Value = customColorControl.DisplayColor.Color.R;
                customColorControl.SliderG.Value = customColorControl.DisplayColor.Color.G;
                customColorControl.SliderB.Value = customColorControl.DisplayColor.Color.B;
                customColorControl.SliderA.Value = customColorControl.DisplayColor.Color.A;
            }
        }
    }
}
